// ----------------------------------------------------------------------------
// Classname:		CApp
// Created:			July 7, 2010
//
// Description:		This is the main class for the application / game.
//					Initialization, loading resources, and running the game
//					loop will all happen within this class.
// Notes:	This class is created as a Singleton to prevent more then one 
//			instance.  There can only be one instance of this class running.
// ----------------------------------------------------------------------------

#include "headers/Tigerengine.h"

// ----------------------------------------------
// Init()
//
//   Initialise the sub systems within the App
//   class.  Then set flags to indicate that game
//   is active (in focus) and running.
// ----------------------------------------------
int CApp::Init(char *psAppConfig)
{
	int		retValue;			// used for error checking

	// Init the Config system
	//		System for loading and parsing config and data files

	// Load the config file for the game.  This is a main data file
	// that has the game settings suchs as screen info, etc.
	// m_pConfig->LoadInitConfig(psAppConfig);

	// Init the input system
	//		This system will setup the keyboard key map, and gamepad
	//		button map.
	m_input = new CInput();
	if(m_input == NULL)
	{
		DBGMSG("Cannot allocate memory for CApp::m_input");
		return RET_ERR_OUTOFMEM;
	} // if(m_input == NULL)

	retValue = m_input->Init(TE3D_KEYBOARD);
	if(retValue != RET_SUCCESS)
	{
		DBGMSG("Error calling Init method for input Subsystem.");
		return RET_FAILURE;
	} // if(retValue != RET_SUCCESS)

	// Init the display system.
	//		Use the loaded data from the config system
	m_display = new CDisplay();
	if(m_display == NULL)
	{
		DBGMSG("Cannot allocate memory for CApp::m_display");
		return RET_ERR_OUTOFMEM;
	} // if(m_display == NULL)
	
	m_display->InitDisplay(300, 200, 32, 0, __TEXT("Test"));	

	m_bActive = true;
	m_bRunning =  true;

	return RET_SUCCESS;
} // void CApp::Init(void)

// ----------------------------------------------
// Gameloop()
//
//		This is the main Gameloop.  Everything
//		that happens within a frame is here.
// ----------------------------------------------
void CApp::Gameloop(void)
{
	// 1. Check and process player input
	// 2. Run AI
	// 3. Render Screen
	
	if(m_bActive)		// Only if game is currently active
	{
		glClear(GL_COLOR_BUFFER_BIT);
		glLoadIdentity();

		glBegin(GL_TRIANGLES);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2i(10, 10);
		glVertex2i(40, 50);
		glVertex2i(5, 45);
		glEnd();

		// After all processing, show the display.
		m_display->Flip();
	} // if(m_bActive)
} // void CApp::Gameloop(void)

// ----------------------------------------------
// Shutdown()
//
// ----------------------------------------------
void CApp::Shutdown(void)
{
	m_display->ShutdownDisplay();
	delete m_display;

	m_bActive = FALSE;
	m_bRunning = FALSE;
} // void CApp::Shutdown(void)


CApp::CApp(void)
{
	m_bActive = false;
	m_bRunning = false;
	m_display = NULL;
}

CApp::~CApp(void)
{
}
